<?php
namespace app\command\generate;

use app\command\Generate;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\Db;

class Clear extends Generate {

    protected function configure()
    {
        parent::configure();
        $this->addArgument("table",Argument::OPTIONAL,"the name of table");
        $this->setName('generate:resource:clear')->setDescription('clear backend controller model validate service ');
    }

    protected function getStub()
    {
        // TODO: Implement getStub() method.
    }


    protected function execute(Input $input, Output $output)
    {
        $name = $input->getArgument("name");
        $table= $input->getArgument("table");
        if(empty($table)) $table = $name;
        $name = ucfirst($name);
        $table = ucfirst($table);

        $controllerBasePath1 = APP_PATH.'admin'.DS.'controller'.DS.$name.'.php';
        $controllerBasePath2 = APP_PATH.'common'.DS.'model'.DS.$table.'.php';
        $controllerBasePath3 = APP_PATH.'common'.DS.'validate'.DS.$table.'.php';
        $controllerBasePath4 = APP_PATH.'common'.DS.'service'.DS.$table.'.php';

        $list = [$controllerBasePath1,$controllerBasePath2,$controllerBasePath3,$controllerBasePath4];
        foreach($list as $val){
            if(file_exists($val)){
                $content = file_get_contents($val);
                if(preg_match("/created by command generate/",$content)){
                    unlink($val);
                    $output->writeln($val." delete success!");
                }
            }
        }





    }

}